﻿using DotNet.Helper;
using DotNet.Mvc;
using DotNet.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XNWebModel;
using XNWebService;

namespace XNWebAPI.Controllers
{
    public class TccDictionaryController : JsonController
    {
        private readonly SystemDictionaryService service = new SystemDictionaryService();

        /// <summary>
        /// 根据code值查询其树下的所有节点
        /// </summary>
        /// <param name="dicCode">节点的code值</param>
        /// <returns></returns>
        public JsonNetResult GetRecursiveDictionary(string dicCode)
        {
            TreeData treed = new TreeData();

            RecursiveDictionary(dicCode,ref treed);

            if (treed == null)
            {

                return Json(new BoolMessage<dynamic>(false, "无效的编码值"));
            }
            else
            {
                return Json(new BoolMessage<dynamic>(treed));
            }

        }

        /// <summary>
        /// 查询树型下数据
        /// </summary>
        /// <param name="pcode"></param>
        /// <param name="treedata"></param>
        /// <returns></returns>
        private void RecursiveDictionary(string pcode,ref TreeData treedata)
        {
            LogHelper.Log(pcode);
            var pdic = service.GetDiction(pcode);

            List<dynamic> sonList = new List<dynamic>();
            if (pdic.Success)
            {

                treedata.Id = pdic.Data.Id;
                treedata.Name = pdic.Data.Name;
                treedata.Code = pdic.Data.Code;
                treedata.Dicdata = service.GetDictionDetails(pdic.Data.Id).Data;


                BoolMessage<List<SystemDictionaryDetails>> lDetails = null;

                lDetails = service.GetDictionDetails(pdic.Data.Id);

                var pdicl = service.GetSonDiction(pdic.Data.Id);
                if (pdicl.Success)
                {
                    foreach (var dicl in pdicl.Data)
                    {
                        TreeData newTreeData = new TreeData();
                        newTreeData.Id = dicl.Id;
                        newTreeData.Name = dicl.Name;
                        newTreeData.Code = dicl.Code;
                        newTreeData.Dicdata = service.GetDictionDetails(dicl.Id).Data;
                        RecursiveDictionary(dicl.Code,ref newTreeData);
                        treedata.Sondata.Add(newTreeData);
                        
                    }
                }
            }
            else
            {
                treedata = null;
            }
        }

        private class TreeData
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Code { get; set; }
            public List<SystemDictionaryDetails> Dicdata { get; set; }
            public List<TreeData> Sondata { get; set; }

            public TreeData()
            {
                Sondata = new List<TreeData>();
            }
        }

        /// <summary>
        /// 根据code值与name值查询对应字典值
        /// </summary>
        /// <param name="dicCode">code值</param>
        /// <param name="name">name值</param>
        /// <returns></returns>
        public JsonNetResult GetDictionaryAccDetails(string dicCode, string name)
        {
            var details = service.GetDetailsNoOrder(dicCode);
            var data = details.Data.Where(p => p.Name == name).SingleOrDefault();
            if (data != null)
            {
                return Json(new BoolMessage<SystemDictionaryDetails>(data));
            }
            else
            {
                return Json(new BoolMessage<SystemDictionaryDetails>(false, "未找到相应项"));
            }
        }


    }
}